package com.fakeDYChat.common.common.event.listener;

import com.fakeDYChat.common.common.event.UserRegisterEvent;
import com.fakeDYChat.common.user.dao.UserDao;
import com.fakeDYChat.common.user.domain.entity.User;
import com.fakeDYChat.common.user.domain.enums.IdempotentEnum;
import com.fakeDYChat.common.user.domain.enums.ItemEnum;
import com.fakeDYChat.common.user.service.IUserBackpackService;
import com.fakeDYChat.common.user.service.IUserService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Optional;

@Service
public class UserRegisterListener {

    private final IUserBackpackService userBackpackService;
    private final UserDao userDao;

    public UserRegisterListener(IUserBackpackService userBackpackService, UserDao userDao) {
        this.userBackpackService = userBackpackService;
        this.userDao = userDao;
    }

    /**
     * 接收用户注册事件（用户注册事件已提交）并通过异步执行发送改名卡的逻辑
     * 可靠分析：该逻辑为不必须逻辑，此逻辑不能影响用户的正常注册逻辑。
     * 且在输入合法的情况下，该逻辑出错的情况有数据库出错和服务器宕机
     *
     * @param userRegisterEvent 用户注册事件
     * @author Tang
     * @date 2024/1/4 16:11
     **/
    @Async
    @TransactionalEventListener(classes = UserRegisterEvent.class, phase = TransactionPhase.AFTER_COMMIT)
    public void sendModifyCard(UserRegisterEvent userRegisterEvent) {
        User user = userRegisterEvent.getUser();
        Optional.ofNullable(user)
                .ifPresent(i -> userBackpackService.acquireItem(user.getId(),
                        ItemEnum.MODIFY_NAME_CARD.getId(),
                        IdempotentEnum.UID,
                        String.valueOf(user.getOpenId())));
    }

    /**
     * 接收用户注册事件（用户注册事件已提交）并通过异步执行发送注册徽章（前10和前100）的逻辑
     *
     * @param userRegisterEvent 用户注册事件
     * @author Tang
     * @date 2024/1/4 16:11
     **/
    @TransactionalEventListener(classes = UserRegisterEvent.class, phase = TransactionPhase.AFTER_COMMIT)
    public void sendRegisterBadge(UserRegisterEvent userRegisterEvent) {
        User user = userRegisterEvent.getUser();
        Optional.ofNullable(user)
                .ifPresent(i -> {
                    Long id = user.getId();
                    Integer ith = userDao.getIthRegister(id);
                    //根据用户是第几个注册的，发放对应的徽章
                    if (ith <= 10) {
                        userBackpackService.acquireItem(user.getId(),
                                ItemEnum.REG_TOP10_BADGE.getId(),
                                IdempotentEnum.UID,
                                String.valueOf(user.getOpenId()));
                    } else if (ith <= 100) {
                        userBackpackService.acquireItem(user.getId(),
                                ItemEnum.REG_TOP100_BADGE.getId(),
                                IdempotentEnum.UID,
                                String.valueOf(user.getOpenId()));
                    }
                });
    }
}
